package com.vframework.system.operatelog.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vframework.base.controller.BaseController;
import com.vframework.base.exception.BaseException;
import com.vframework.system.operatelog.model.OperateLogBean;
import com.vframework.system.operatelog.service.OperateLogService;

/***************************************************************
 * Name:        OperateLogController.java
 * Create At:   2018年5月24日
 * Module:      
 * Description: [系统操作--操作日志查询]
 * @author      ztshan
 * ------------------------------------------------------------
 * 修改历史
 * 序号         日期                       修改人               修改原因
 * 1    2018年5月24日      ztshan    
 * 2
***************************************************************/
@Controller
@RequestMapping("OperateLogController")
public class OperateLogController extends BaseController {

    private static final String BASE_PATH = "com/vframework/system/operatelog/pages/";
    
	@Autowired
	private OperateLogService operateLogService;
	 
	/**
	* Author:miaowenqiang
	* Create At:2012-5-11
	* Description: [返回操作日志信息页面]
	* @return String
	 * @throws Exception 
	*/
	@RequestMapping(value = "begin")
	private String begin(Model model) throws Exception {
		return queryList(model);
	}
	
	@RequestMapping(value = "queryList")
	private String queryList(Model model) throws Exception {
		try {
		    List<OperateLogBean> list = new ArrayList<OperateLogBean>();
		    int pageCount = 0;
		    pageCount = operateLogService.queryOperateLogCount(super.paramMap);
			if(pageCount>0){
				super.setPageInfo(pageCount);
				list = operateLogService.queryOperateLogList(super.paramMap);
			}
			model.addAttribute("list", list);
			request.setAttribute("pageCount",pageCount);
			request.setAttribute("actionurl","OperateLogController/queryList.do");
		} catch (Exception e) {
		    super.message("查询失败: " + e.getMessage());
			return BASE_PATH + "operateLog";
		}
		return BASE_PATH + "operateLog";
	}

	/**
	* Author:miaowenqiang
	* Create At:2012-5-11
	* Description: [导出操作日志信息页面]
	* @return String
	 * @throws Exception 
	*/
	@RequestMapping(value = "exportOperateLog")
	private String exportOperateLog(Model model, HttpServletResponse response) throws Exception {
		try {
			/*super.paramMap.put("param_curpage", 0);
			super.paramMap.put("param_ipagecount", 20000);
			List<OperateLogBean> resultList = operateLogService.queryOperateLogList(super.paramMap);
			int len = resultList.size();
			Object[][] data = new Object[len+1][15];
			data[0] =new Object[]{"流水号","菜单名称","操作描述","操作时间","操作人","操作人帐号","IP地址","操作类型"};
			
			int i = 1;
			for(OperateLogBean olb : resultList){
				data[i][0] = olb.getOperateId();
				if(olb.getMenuId() == null || "".equals(olb.getMenuId())){
					data[i][1] = "登录";
				}
				else{
					data[i][1] = olb.getMenuId();
				}
				data[i][2] = olb.getRemark();
				data[i][3] = olb.getOpdate();
				data[i][4] = olb.getOperator();
				data[i][5] = olb.getOperatorid();
				data[i][6] = olb.getIp();
				data[i][7] = olb.getOperateType();
				i++;
			}
			
			ExportBean excelBean = new ExportBean();
			excelBean.setData(data);
			excelBean.setExcelName("操作日志信息");
			excelBean.setSheetName("操作日志信息");
			excelBean.setExportMode(0);
			new ExportExcelJxl(response, excelBean);
			super.message("导出成功");*/
			operateLogService.insertLog(super.MakeLogBean(super.LOG_IMPORT_KEY, "导出日志", "导出操作日志信息"));
		} catch (BaseException e) {
			super.message("导出操作日志信息失败: " + e.getMessage());
			return queryList(model);
		}
		return null;
	}
	
	/**
	* Author:miaowenqiang
	* Create At:2012-5-11
	* Description: [导出操作日志信息页面]
	* @return String
	 * @throws Exception 
	*/
	@RequestMapping(value = "deleteOperateLog")
	private String deleteOperateLog(Model model) throws Exception {
		try {
			operateLogService.clearOperateLog((String)super.paramMap.get("cur_orgId"));
			super.message("删除成功！");
		} catch (BaseException e) {
			super.message("删除失败: " + e.getMessage());
			return queryList(model);
		}
		operateLogService.insertLog(super.MakeLogBean(super.LOG_DELETE_KEY, "删除日志", super.paramMap.toString()));
		return queryList(model);
	}
	
}
